package com.exercise.widgets.menu

import android.content.Context
import android.content.Intent
import android.view.ActionMode
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel
import com.exercise.widgets.R
import com.exercise.widgets.databinding.ActivityOptionMenuBinding

class OptionMenuActivity : BaseActivity<BaseViewModel, ActivityOptionMenuBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivityOptionMenuBinding
        get() = ActivityOptionMenuBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java

    override fun initData() {
    }

    override fun initView() {
        setSupportActionBar(binding.toolbar)
        binding.toolbar.setNavigationOnClickListener { finish() }
        binding.fab.setOnLongClickListener {
            when (actionMode) {
                null -> {
                    actionMode = binding.toolbar.startActionMode(actionModeCallback)
                    it.isSelected = true
                    true
                }
                else -> false
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_file_option, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.open_file -> {
                "Open File".toast()
                true
            }

            R.id.create_new -> {
                "Create File".toast()
                true
            }

            R.id.download -> {
                "Download".toast()
                true
            }

            R.id.edit -> {
                "Edit".toast()
                true
            }

            else -> super.onOptionsItemSelected(item)
        }
    }

    private var actionMode: ActionMode? = null
    private val actionModeCallback = object : ActionMode.Callback {
        override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
            mode?.menuInflater?.inflate(R.menu.menu_pop_menu, menu)
            return true
        }

        override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
            return false
        }

        override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
            return when (item?.itemId) {
                 R.id.action_delete-> {
                    "ActionMode: delete".toast()
                     mode?.finish()
                     true
                 }
                R.id.action_favorite ->{
                    "ActionMode: favorite".toast()
                    true
                }
                else -> false
            }
        }

        override fun onDestroyActionMode(mode: ActionMode?) {
            actionMode = null
            return
        }
    }

    private fun String.toast() {
        Toast.makeText(this@OptionMenuActivity, ">> $this <<", Toast.LENGTH_SHORT).show()
    }

    companion object {
        fun start(context: Context) {
            Intent(context, OptionMenuActivity::class.java).also {
                context.startActivity(it)
            }
        }
    }
}